import { _decorator, Component, Node ,sp} from 'cc';
import *as cc from "cc";
import { EDITOR } from 'cc/env';
const { ccclass, property } = _decorator;

@ccclass('GameMain')
export class MainWork extends Component {
    start() {
        // this.addEventHandler(this.node,this.node);
    }

    // protected addEventHandler(node:Node,FirstNode){
    //     let info = node.name.split('_')
    //     if (info.length > 1) {
    //         let name = node.name
    //         let type = info[0]
    //         switch (type) {
    //             case 'node':
    //                 this[name] = node
    //                 break
    //             case 'btn':
    //                 this[name] = node.getComponent(cc.Button)
    //                 if (this[name]) {
    //                     var eventHandler = new cc.Component.EventHandler();
    //                     eventHandler.target = FirstNode;
    //                     eventHandler.component = cc.js.getClassName(this);
    //                     eventHandler.handler = "onBtnClicked";
    //                     eventHandler.customEventData = name;
    //                     var clickEvents = node.getComponent(cc.Button).clickEvents;
    //                     clickEvents.push(eventHandler);
    //                 }
    //                 break
    //             case 'progress':
    //                 this[name] = node.getComponent(cc.ProgressBar)
    //                 break
    //             case 'txt':
    //             case 'lbl':
    //                 this[name] = node.getComponent(cc.Label)
    //                 break
    //             case 'sp':
    //                 this[name] = node.getComponent(cc.Sprite)
    //                 break
    //             case 'spine':
    //                 this[name] = node.getComponent(sp.Skeleton)
    //                 break
    //             case 'tog':
    //                 this[name] = node.getComponent(cc.Toggle);
    //                 if (this[name]) {
    //                     var eventHandler = new cc.Component.EventHandler();
    //                     eventHandler.target = FirstNode;
    //                     eventHandler.component = cc.js.getClassName(this);
    //                     eventHandler.handler = "onBtnClicked";
    //                     eventHandler.customEventData = name;
    //                     var clickEvents = node.getComponent(cc.Toggle).clickEvents;
    //                     clickEvents.push(eventHandler);
    //                 }
    //                 break
    //             case 'toc':
    //                 this[name] = node.getComponent(cc.ToggleContainer);
    //                 break;
    //             case 'page':
    //                 this[name] = node.getComponent(cc.PageView)
    //                 break;

    //         }
    //     }
    //     for (const item of node.children) {
    //         this.addEventHandler(item,FirstNode)
    //     }
    // }

    BtnCODE(event, customEventData) {
        // console.log( ' 调用');
        var btnName = event.target.name;
        switch (btnName) {
            case "btn_close":
                this.closeThisNode();
                break;
        }

    }

    onLoad() {
        if (this.node.getComponent(cc.Button)) {
            console.log("+++++++++++++");
            this.addBtnEvent(this.node, this.node, cc.js.getClassName(this), "BtnCODE", this.node.getComponent(cc.Button).target);
        }
        this.AddBtn(this.node);
    }

    IsSkipNode(node: cc.Node): boolean {
        if (this.node == node) {
            return false;
        }
        let b = node.getComponent( MainWork );
        return b != null;
    }

    //添加按钮事件监测
    AddBtn(node) {
        if (this.IsSkipNode(node)) return;

        for (var i = 0; i < node.children.length; ++i) {
            var tmp = node.children[i];
            if (this.IsSkipNode(tmp)) continue;
            if (tmp.getComponent(cc.Button)) {
                // console.log("--------------------------FIND BUTTON-----------------------");
                this.addBtnEvent(tmp, this.node, cc.js.getClassName(this), "BtnCODE", tmp.getComponent(cc.Button).target);
            }
            this.AddBtn(tmp);
        }
    }

    closeThisNode(){
        // console.log("--------------------------CLOSE-----------------------");
        // gameShareAdsVideo.bannerAd1Hide();
        // cc.tween(this.viewNode).to(0.5,{y:this.node.getPosition().y + 1600}, { easing: 'backInOut' }).call(()=>{
        //     this.node.active = false;
        // }).start();
        

    }

    //__________________________________________________________________________________________________________________________________________________________________________________________________________________________
    //添加按钮绑定
    public addBtnEvent(node, target, component, handler, customEventData) {
        var eventHandler = new cc.Component.EventHandler();
        eventHandler.target = target;
        eventHandler.component = component;
        eventHandler.handler = handler;
        if (customEventData) eventHandler.customEventData = customEventData;

        var clickEvents = node.getComponent(cc.Button).clickEvents;
        if (clickEvents.length > 0) {
            if (!EDITOR)
                // cc.warn("按钮已经存在绑定，跳过自动绑定", node.name);
            return;
        }
        //console.log(node.name,target.name,component) ;
        clickEvents.push(eventHandler);
    }


    protected rnd(n, m){
        var random = Math.floor(Math.random()*(m-n+1)+n);
        return random;
    }

    protected shuffle(array) {
        var m = array.length,
            t, i;
        while (m) {
            i = Math.floor(Math.random() * m--);
            t = array[m];
            array[m] = array[i];
            array[i] = t;
        }
        return array;
    }



    // update(deltaTime: number) {
        
    // }
}

